﻿//==================================================================
// Copyright (C) 2011 iSun Studio 软件工作室
// 文件名: Set_EditUICofig.cs
// 作 者：代码自动生成
// 日 期：2011-10-25 9:42:23
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using lite;
using Csla;
using System.ComponentModel;
using System;
using OpenExpressApp.ORM;
using System.Data.OleDb;
using Csla.Data;

namespace iSun.Model
{
	/// <summary>
    /// 
    /// </summary>
	[Serializable]
    [lite.Table(Name="Set_EditUIConfig")]
    [iSun.Model.Table("Set_EditUIConfig","EditConfigID")]
	public class EditUIConfig: iSun.Model.BaseEntity
	{
        
        #region Business Methods 业务方法
       
		/// <summary>
        /// 
        /// </summary>


        [Column, PK]
        public Guid EditConfigID
        {
            get;
            set;
        }
        
		/// <summary>
        /// 实体名
        /// </summary>
        // private static PropertyInfo< string > EntityNameProperty = RegisterProperty(new PropertyInfo< string >("EntityName"));

        [Column]
        public string EntityName
        {
            get;
            set;
        }
		/// <summary>
        /// 名称
        /// </summary>


        [Column]
        public string Caption
        {
            get;
            set;
        }

        [Column]
        public string DataSourceType
        {
            get;
            set;
        }
        /// <summary>
        /// 组合框类型 U:DataUser 
        /// </summary>
        [Column]
        public string ZType
        {
            get;
            set;
        }

        [Column]
        public int ColSpan
        {
            get;
            set;
        }
		/// <summary>
        /// 字段名称
        /// </summary>
        //    private static PropertyInfo< string > FieldNameProperty = RegisterProperty(new PropertyInfo< string >("FieldName"));

        [Column]
        public string FieldName
        {
            get;
            set;
        }
		/// <summary>
        /// 排序
        /// </summary>
      //  private static PropertyInfo< int > iOrderProperty = RegisterProperty(new PropertyInfo< int >("iOrder"));

        [Column]
        public int iOrder
        {
            get;
            set;
        }
		/// <summary>
        /// 控制类型
        /// </summary>
        // private static PropertyInfo< string > ControlTypeProperty = RegisterProperty(new PropertyInfo< string >("ControlType"));

        [Column]
        public string ControlType
        {
            get;
            set;
        }
		/// <summary>
        /// 
        /// </summary>
        ///private static PropertyInfo< string > IsVisiableProperty = RegisterProperty(new PropertyInfo< string >("IsVisiable"));

        [Column]
        public string IsVisiable
        {
            get;
            set;
        }
		/// <summary>
        /// 
        /// </summary>
        ///private static PropertyInfo< int > LengthProperty = RegisterProperty(new PropertyInfo< int >("Length"));

        [Column]
        public int Length
        {
            get;
            set;
        }
		/// <summary>
        /// 
        /// </summary>
        //private static PropertyInfo< string > DataSourceProperty = RegisterProperty(new PropertyInfo< string >("DataSource"));

        [Column]
        public string DataSource
        {
            get;
            set;
        }

        [Column]
        public int ColumnIndex
        {
            get;
            set;
        }

        [Column]
        public int Rows
        {
            get;
            set;
        }

        [Column]
        public int PositionX
        {
            get;
            set;
        }

        [Column]
        public int PositionY
        {
            get;
            set;
        }
        [Column]
        public int IsRequire
        {
            get;
            set;
        }

        [Column]
        public string IsSysAutoSave
        {
            get;
            set;
        }


        #endregion
        
        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "EntityName");
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("EntityName", 100));
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Caption");
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Caption", 100));
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "FieldName");
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("FieldName", 100));
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "ControlType");
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ControlType", 1));
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "IsVisiable");
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("IsVisiable", 1));
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "DataSource");
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("DataSource", 500));
            //ValidationRules.AddRule(Csla.Validation.CommonRules.Equals,
            //base.AddBusinessRules();
        }

        /// <summary>
        /// 验证规则与业务对象
        /// </summary>
        /// <returns></returns>
        public override string CheckData()
        {
            return base.CheckData();
        }

        #endregion
        
        #region Factory Methods 工厂方法

        public EditUIConfig(){}

        public static EditUIConfig New()
        {
            return DataPortal.Create< EditUIConfig>();
        }
        public static EditUIConfig Get(Guid EditConfigID)
        {
            return DataPortal.Fetch< EditUIConfig>(new SingleLazyCriteria< EditUIConfig, Guid>(EditConfigID, false));
        }
        public static void Delete(Guid EditConfigID)
        {
            DataPortal.Delete(EditConfigID);
        }

        //public override Set_EditUICofig Save()
        //{
            //return base.Save();
        //}
        #endregion
		
		#region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid EditConfigID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
            using (var ctx = GetConnectionManager())
            {
                IDb db = DbFactory.Instance.GetDb(ctx);
                IQuery q = db.Query();
                q.Constrain("EditConfigID").Equal(EditConfigID);
                var data = db.Select<EditUIConfig>(q)[0];
                //LoadProperty(EditConfigIDProperty, data.EditConfigID);
                //LoadProperty(EntityNameProperty, data.EntityName);
                //LoadProperty(CaptionProperty, data.Caption);
                //LoadProperty(FieldNameProperty, data.FieldName);
                //LoadProperty(iOrderProperty, data.iOrder);
                //LoadProperty(ControlTypeProperty, data.ControlType);
                //LoadProperty(IsVisiableProperty, data.IsVisiable);
                //LoadProperty(LengthProperty, data.Length);
                //LoadProperty(DataSourceProperty, data.DataSource);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(this.EditConfigID);
        }
        #endregion

	}
}

